﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using Microsoft.Web.Mvc;
using SinsenWeb.Models;

namespace System.Web.Mvc
{
    public static class HtmlExtensions
    {
        public static string DeleteForm(this HtmlHelper html, string controller, int id)
        {
            var url = new UrlHelper(html.ViewContext.RequestContext);
            string postAction = url.Action("Delete", controller, new { id });

            string template = "<form action=\"{0}\" method=\"post\" onsubmit=\"return confirmDelete()\">";
            string formstart = string.Format(template, postAction);
            var formContent = @"<input type=""image"" src=""/Content/Images/delete.gif""/> ";
            var formEnd = "</form>";
            return formstart + formContent + formEnd;
        }

        public static string EditImageLink(this HtmlHelper html, string controller, int id)
        {
            var url = new UrlHelper(html.ViewContext.RequestContext).Action("Edit", controller, new { id });

            return string.Format(@"<a href=""{0}""><img src=""/Content/Images/edit.gif"" /></a>", url);
        }
        public static string ImageLink<T>(this HtmlHelper helper, Expression<Action<T>> linkUrlAction, string imageUrlPath, string linkTarget, string altText) where T : Controller
        {
            string linkUrl = helper.BuildUrlFromExpression(linkUrlAction);

            string outputUrl = string.Format(@"<a href='{0}' target='{1}'><img src='{2}' alt='{3}' style='border-style: none' /></a>", linkUrl, linkTarget, imageUrlPath, altText);
            return outputUrl;
        }


    }
}
